<script setup>
import { ref } from 'vue'
import {
    Edit,
    Delete
} from '@element-plus/icons-vue'
import { addAStudent, deleteAStudent, getStudents, searchAStudent, updateAStudent } from '@/api/student';
import { ElMessage, ElMessageBox } from 'element-plus';

// 搜索学员
const studentSearch = ref('')
// 学员数据
const studentsData = ref([])
//分页条数据模型
const pageNum = ref(1)//当前页
const total = ref(0)//总条数
const pageSize = ref(6)//每页条数
//控制弹窗标题
const title = ref('')


// 学员表单模型
const studentModel = ref({
    name: "",
    gender: "",
    phone: "",
    membership_start: "",
    membership_end: ""
})
//控制抽屉是否显示
const visibleDrawer = ref(false)
// 控制弹窗显示
const dialogVisible = ref(false)
// 加载中
const loading = ref(false)
// 分页控制
const handleCurrentChange = (val) => {
    pageNum.value = val
    initStudentData()
}



// 添加学员
const addStudent = async () => {
    let result = await addAStudent(studentModel.value);
    if (result.data.code == 0) {
        ElMessage.success(result.data.msg)
        visibleDrawer.value = false
    } else {
        ElMessage.error(result.data.msg)
    }
}

// 搜索学员
const searchStudent = async () => {
    let student = await searchAStudent(studentSearch.value);
    if (student.data.code == 0) {
        studentsData.value = student.data.data;
        total.value = student.data.total
    } else {
        ElMessage.warning(student.data.msg)
    }
}
// 修改学员弹窗展示
const showUpdateStudent = (row) => {
    dialogVisible.value = true;
    studentModel.value.name = row.name;
    studentModel.value.gender = row.gender;
    studentModel.value.phone = row.phone;
    studentModel.value.membership_start = row.membership_start;
    studentModel.value.membership_end = row.membership_end;
    studentModel.value.id = row.id;
}
// 修改学员
const updateStudent = async () => {
    let student = await updateAStudent(studentModel.value);
    ElMessage.success(student.data.msg)
    dialogVisible.value = false
    initStudentData()
}
// 删除学员
const deleteStudent = async (row) => {
    ElMessageBox.confirm(
        '你确认要删除该学员吗?',
        '温馨提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(async () => {
            //调用接口
            let result = await deleteAStudent(row.id);
            console.log(result);
            ElMessage({
                type: 'success',
                message: '删除成功',
            })
            //刷新列表
            initStudentData();
        })
        .catch(() => {
        })

}

// 请求数据
const initStudentData = async () => {
    loading.value = true
    let students = await getStudents(pageNum.value);
    studentsData.value = students.data.data
    total.value = students.data.total
    loading.value = false
}
initStudentData()
</script>

<template>
    <el-card class="page-container" body-style="padding: 10px 15px;">
        <!-- 头部标题 -->
        <template #header>
            <div class="header">
                <span style="font-weight:800">学员管理</span>
                <div class="extra">
                    <el-button type="primary" @click="visibleDrawer = true;">添加学员</el-button>
                </div>
            </div>
        </template>
        <!-- 中间表格 -->
        <el-table :data="studentsData" v-loading="loading" stripe style="width: 100%" height="380"
            :row-style="{ height: '55px' }">
            <el-table-column label="学员" prop="name"></el-table-column>
            <el-table-column label="性别" prop="gender"></el-table-column>
            <el-table-column label="手机号" prop="phone"> </el-table-column>
            <el-table-column label="会员开始时间" prop="membership_start"></el-table-column>
            <el-table-column label="会员结束时间" prop="membership_end"></el-table-column>
            <el-table-column width="130">
                <template #header>
                    <el-input v-model="studentSearch" placeholder="输入学员名称" @keyup.enter="searchStudent" />
                </template>
                <template #default="{ row }">
                    <el-button :icon="Edit" circle plain type="primary" @click="showUpdateStudent(row)"></el-button>
                    <el-button :icon="Delete" circle plain type="danger" @click="deleteStudent(row)"></el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 页码 -->
        <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" layout="total, prev, pager, next"
            background :total="total" style="margin-top: 20px; justify-content: flex-end"
            @current-change="handleCurrentChange" />
        <!-- 抽屉 -->
        <el-drawer v-model="visibleDrawer" title="添加学员" direction="rtl" size="35%">
            <!-- 添加表单 -->
            <el-form :model="studentModel" label-width="100px">
                <el-form-item label="学员姓名">
                    <el-input v-model="studentModel.name" placeholder="请输入学员姓名"></el-input>
                </el-form-item>
                <el-form-item label="性别">
                    <el-radio-group v-model="studentModel.gender">
                        <el-radio value="男" size="large">男</el-radio>
                        <el-radio value="女" size="large">女</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="手机号">
                    <el-input v-model="studentModel.phone" placeholder="请输入学员手机号"></el-input>
                </el-form-item>
                <el-form-item label="会员开始日期">
                    <el-date-picker v-model="studentModel.membership_start" type="date" placeholder="开始时间" size="large"
                        format="YYYY/MM/DD" value-format="YYYY-MM-DD" />
                </el-form-item>
                <el-form-item label="会员结束日期">
                    <el-date-picker v-model="studentModel.membership_end" type="date" placeholder="结束时间" size="large"
                        format="YYYY/MM/DD" value-format="YYYY-MM-DD" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="addStudent">提交</el-button>
                </el-form-item>
            </el-form>
        </el-drawer>
        <!-- 修改弹窗 -->
        <el-dialog v-model="dialogVisible" title="修改学员" width="30%">
            <!-- 修改表单 -->
            <el-form :model="studentModel" label-width="100px">
                <el-form-item label="学员姓名">
                    <el-input v-model="studentModel.name" placeholder="请输入学员姓名"></el-input>
                </el-form-item>
                <el-form-item label="性别">
                    <el-radio-group v-model="studentModel.gender">
                        <el-radio value="男" size="large">男</el-radio>
                        <el-radio value="女" size="large">女</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="手机号">
                    <el-input v-model="studentModel.phone" placeholder="请输入学员手机号"></el-input>
                </el-form-item>
                <el-form-item label="会员开始日期">
                    <el-date-picker v-model="studentModel.membership_start" type="date" placeholder="开始时间" size="large"
                        format="YYYY/MM/DD" value-format="YYYY-MM-DD" />
                </el-form-item>
                <el-form-item label="会员结束日期">
                    <el-date-picker v-model="studentModel.membership_end" type="date" placeholder="结束时间" size="large"
                        format="YYYY/MM/DD" value-format="YYYY-MM-DD" />
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="updateStudent">确认</el-button>
                </span>
            </template>
        </el-dialog>
    </el-card>
</template>


<style lang="scss" scoped>
/* 主容器 */
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }

}

/* 清除浮动 */
.clearfix {
    clear: both;
}
</style>